﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BtoBSWT315.Admin
{
    public partial class UpgradeMessageList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            // 在此处放置用户代码以初始化页面
            if (Session["AdminName"] == null)
            {
                Response.Redirect("Admin_Login.aspx");
            }

            if (!IsPostBack)
            {
                if (Request.QueryString["State"] == null || Request.QueryString["State"].ToString() == String.Empty)
                {
                    Response.Write("错误!! 参数出了问题..");
                    Response.End();
                }

                ViewState["MState"] = Request.QueryString["State"].ToString();

                if (ViewState["MState"].ToString().Equals("0"))
                {
                    lblState.Text = "未读信息";
                }
                else
                {
                    lblState.Text = "已读信息";
                }
                getDataCount();//获取记录个数
                getData();
            }
        }

        private void getDataCount()//获取记录个数
        {
            string mySql = "select count(*) as [id] from [Upgrade] where MState=" + ViewState["MState"].ToString();
            Admin.Admin_ClassDb mydb = new Admin.Admin_ClassDb();
            lblNum.Text = mydb.GetdrPurview(mySql);
        }



        private void getData()//绑定数据
        {
            Admin.Admin_ClassDb mydb = new Admin.Admin_ClassDb();
            string mysql = "select * from Upgrade where MState=" + ViewState["MState"].ToString() + " order by id desc";

            messageDataGrid.DataSource = mydb.GetTables(mysql).DefaultView;
            messageDataGrid.DataBind();
        }

        #region Web 窗体设计器生成的代码
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
        /// 此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.messageDataGrid.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.messageDataGrid_DeleteCommand);
            this.messageDataGrid.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.messageDataGrid_ItemDataBound);

        }
        #endregion

        private void messageDataGrid_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                //删除确认            
                LinkButton delBttn = (LinkButton)e.Item.Cells[6].Controls[0];
                delBttn.Attributes.Add("onclick", "javascript:return confirm('确定删除[ " + e.Item.Cells[1].Text + " ]?');");
            }
        }

        private void messageDataGrid_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
        {
            string strid = e.Item.Cells[0].Text;
            Admin.Admin_ClassDb mydb = new Admin.Admin_ClassDb();

            string sql = "Delete from Upgrade where id=" + strid;
            mydb.GetNonResult(sql);
            getData();
        }
    }
}